package com.shujia.mysql;

import java.sql.*;
/**
 * @Description: 使用Statement
 * @param:
 * @return:
 * @auther: 王火火
 * @date: 2019/9/6 10:38
 */
public class MysqlJDBC {
    //创建常量
    static final String DRIVER="com.mysql.jdbc.Driver";//驱动
    static final String URL="jdbc:mysql://master:3306/shujia";//路劲
    static final String USERNAME="root";//用户名
    static final String PASSWORD="123456";//密码
    public static void main(String[] args) throws Exception{
        //1.加载驱动 反射加载驱动
        Class.forName(DRIVER);
        //2.获取连接
        Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        //3.执行查询 通过conn获取执行器
        Statement statement = conn.createStatement();

        //4.要执行的sql语句
        //查询ename=SMITH
        String name="SMITH";
        String sql="select * from emp where ename='"+name+"'";
        System.out.println(sql);
        //执行获取结果
        //execute();用来执行删除更新添加
        //executeQuery();执行查询
        ResultSet rs = statement.executeQuery(sql);
        //5.循环输出结果(展开结果集)
        while(rs.next()){
            //获取mysql中具体的值
            int empno = rs.getInt("EMPNO");
            String ename = rs.getString("ENAME");
            String job = rs.getString("JOB");
            String mgr = rs.getString("MGR");
            Date hiredate = rs.getDate("HIREDATE");
            int sal = rs.getInt("SAL");
            int comm = rs.getInt("COMM");
            int deptno = rs.getInt("DEPTNO");
            System.out.println(empno+"--"+ename+"--"+job+"--"
                    +mgr+"--"
                    +hiredate+"--"
                    +sal+"--"
                    +comm+"--"
                    +deptno);
        }

        //6.关闭
        rs.close();
        statement.close();
        conn.close();

    }
}
